{template 'web/'.$cfg['floder'].'/base_menu'}
<div class="top_tab">
    <a href="{php echo $this->createWebUrl('sale')}" class="layui-btn layui-btn-primary">销售报表</a>
    <a href="{php echo $this->createWebUrl('lottery')}" class="layui-btn {if $operation != 'display'}layui-btn-primary{/if}">饿了么-汇总</a>
    <a href="{php echo $this->createWebUrl('sale',array('op'=>'charge'))}" class="layui-btn layui-btn-primary">充值报表</a>


</div>

{if $operation == 'display'}
<style>
    .panelbox-head a{ margin-right:10px}
    .green{ color:#090}
    .orange{ color:#F60}
    .showboxcatainer{display:-moz-box; /* Firefox */  display:-webkit-box; /* Safari and Chrome */  display:box;}
    .showbox{-moz-box-flex:2; /* Firefox */  -webkit-box-flex:2; /* Safari and Chrome */box-flex:2;}
    .showboxtxt{ margin-right: 10px;  background:#EEE; padding:10px}
    .showboxtxt h3{ font-size:12px; text-align:left; line-height:24px;}
    .showboxtxt h3 b{ font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#F60}
    h2{font-size:18px; margin:10px;}
</style>
<div class="panelbox">
    <form action="" method="post" class="layui-form" enctype="multipart/form-data">
        <div class="panelbox-head">
            <a href="javascript:output(0)" class="layui-btn layui-btn-small"><i class="layui-icon">&#xe601;</i> 导出订单</a>

            <a href="javascript:downloadbox()" class="layui-btn layui-btn-normal layui-btn-small"><i class="layui-icon">&#xe601;</i> 同步订单</a>

            <div class="layui-btn-group selecttime">
                <button type="button" class="layui-btn layui-btn-small">今天</button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-small">昨天</button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-small">近7天</button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-small">本月</button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-small">其他</button>
            </div>
            <span id="top_time" style="display:none">{php echo tpl_form_field_daterange('gametime', array('start' => date('Y-m-d'),'end'=>date('Y-m-d')),false)} <button type="button" onclick="getpage(1)" class="layui-btn layui-btn-danger layui-btn-small">查询</button></span>
            <input type="text" name="keyword" value="" placeholder="" />
            <input type="hidden" name="dateselect" value="1" />
        </div>
        <div class="panelbox-body">
            <Div class="showboxcatainer">
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>单数</p>
                        <h3><b id="box1_1">0</b></h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>订单总金额</p>
                        <h3>￥ <b id="box1_2">0</b> </h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>客户实付</p>
                        <h3>￥ <b id="box1_3">0</b> </h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>配送费</p>
                        <h3>￥ <b id="box1_4">0</b> </h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>实际收入</p>
                        <h3>￥ <b id="box1_5">0</b> </h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>客单价</p>
                        <h3>￥ <b id="box1_6">0</b> </h3>
                    </div>
                </div>
                <div class="showbox">
                    <div class="showboxtxt">
                        <p>销售商品总数</p>
                        <h3><b id="box1_7">0</b> 件</h3>
                    </div>
                </div>
            </Div>
        </div>
        <div class="panelbox-body">
            <div class="layui-form">
                <table class="layui-table">
                    <thead>
                    <tr>
                        <th>单号</th>
                        <th>原价</th>
                        <th>实付</th>
                        <th>配送费</th>
                        <th>收入</th>
                        <th>时间</th>
                        <th>姓名</th>
                        <th>地址</th>
                        <th>状态</th>
                        <th>备注</th>
                        <th style="text-align:right">操作</th>
                    </tr>
                    </thead>
                    <tbody id="salelist">
                    </tbody>
                </table>
            </div>
        </div>
        <div class="panelbox-foot text-right">  <div id="pagecount"></div></div>
    </form>
</div>

<div id="editcatebox" style="display:none">
    <form id="editcateform" class="layui-form" action="" style="padding:15px">
        <div class="layui-form-item">
            <select id="dateselector" class="form-control" onchange="showDate();" lay-ignore>
                <option value="1">今天</option>
                <option value="2">昨天</option>
                <option value="3">近7天</option>
                <option value="4">本月</option>
                <option value="5">其他</option>
            </select>
        </div>
        <div class="layui-form-item" id="daterangeslect">
            <input type="text" readonly  class="form-control" id="LAY_demorange_s" value="{php echo date('Y-m-d')}" />
            -
            <input type="text" readonly  class="form-control" id="LAY_demorange_e" value="{php echo date('Y-m-d')}" />
        </div>
    </form>
</div>

<div id="downbox" style="display:none">
    <form id="downboxform" class="layui-form" action="" style="padding:15px">
        <div class="layui-form-item text-center">
            <div class="layui-progress" lay-showPercent="true" lay-filter="pbar">
                <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
            </div>
            <div class="text-center" id="infobox">准备</div>
        </div>
    </form>
</div>
<script language="javascript">
    var myelement;
    layui.use(['layer', 'laypage', 'element','form'], function(){
        var layer = layui.layer
                ,laypage = layui.laypage
                ,element = layui.element
                ,form = layui.form;
        myelement=layui.element;
        getpage(1);
        form.render();
    });
    function showDate(){
        var val=parseInt($("#dateselector :selected").val());
        if(val==5){
            $("#daterangeslect").show();
        }else{
            $("#daterangeslect").hide();
        }
    }
    function downloadbox(){
        $("#editcateform")[0].reset();
        $("#daterangeslect").hide();
        layer.open({
            type: 1,
            title:"选择需要下载的日期",
            btn: ['确认', '关闭'],
            btn1:function(index,layero){
                var val=parseInt($("#dateselector :selected").val());
                if(val==5){
                    var stime=$("#LAY_demorange_s").val();
                    var etime=$("#LAY_demorange_e").val();
                    s1 = new Date(stime.replace(/-/g, "/"));
                    s2 = new Date(etime.replace(/-/g, "/"));
                    var days = parseInt((s2.getTime() - s1.getTime()) / (1000 * 60 * 60 * 24))+1;
                    if(days>31){
                        layer.msg("日期最长不能大于31天",{icon:2,time:2000});
                        return;
                    }
                }
                layer.close(index);
                downshowbox();
            },
            content: $("#editcatebox").show(),
            end:function(){
                $("#editcatebox").hide();
            }
        });
    }
    var dateno=0;
    var datelengt=0;
    var pageindex=0;
    function downshowbox(){
        layer.open({
            type: 1,
            title:"下载数据",
            btn: ['开始下载', '关闭'],
            btn1:function(index,layero){
                dateno=0;
                datelengt=0;
                var val=parseInt($("#dateselector :selected").val());
                var stime,etime;
                switch(val){
                    case 1:
                        stime=etime="{php echo date('Y-m-d')}";
                        datelengt=0;
                        break;
                    case 2:
                        stime=etime="{php echo date('Y-m-d',strtotime('-1 day'))}";
                        datelengt=0;
                        break;
                    case 3:
                        stime="{php echo date('Y-m-d',strtotime('-6 day'))}";
                        etime="{php echo date('Y-m-d')}";
                        datelengt=7;
                        break;
                    case 4:
                        stime="{php echo date('Y-m')}-01";
                        etime="{php echo date('Y-m-d',strtotime('-1 day',strtotime(date('Y-m',strtotime('+1 month')))))}";
                        var s1 = new Date(stime.replace(/-/g, "/"));
                        var s2 = new Date(etime.replace(/-/g, "/"));
                        datelengt = parseInt((s2.getTime() - s1.getTime()) / (1000 * 60 * 60 * 24))+1;
                        break;
                    case 5:
                        stime=$("#LAY_demorange_s").val();
                        etime=$("#LAY_demorange_e").val();
                        var s1 = new Date(stime.replace(/-/g, "/"));
                        var s2 = new Date(etime.replace(/-/g, "/"));
                        datelengt = parseInt((s2.getTime() - s1.getTime()) / (1000 * 60 * 60 * 24))+1;
                        break;
                }
                startDown(stime,etime,1);
            },
            content: $("#downbox").show(),
            success:function(){
                myelement.progress('pbar', '0%');
                $("#infobox").text("准备");
            },
            end:function(){
                $("#downbox").hide();
            }
        });
    }
    function startDown(starttime,endtime,page){
        if(dateno<=datelengt){
            $.post("{php echo $this->createWebUrl('elem',array('op'=>'downdata','shopid'=>$shopid))}",{start:starttime,end:endtime,num:dateno,pageindex:page},function(returnstr) {
                console.log(returnstr);
                var result = eval("(" + returnstr + ")");
                var present=datelengt==0 ? 100 : parseInt(dateno/datelengt*100);
                myelement.progress('pbar', present+'%');
                if (result.success) {
                    if(result.needpage){
                        setTimeout(function(){
                            startDown(starttime,endtime,pageindex++);
                        },300);
                    }else{
                        dateno++;
                        pageindex=1;
                        setTimeout(function(){
                            startDown(starttime,endtime,1);
                        },300);
                    }
                }else{
                    layer.msg(result.msg ? result.msg : returnstr, {icon: 2, time: 2000});
                    return;
                }
            });
        }else{
            layer.msg("下载完成", {icon: 1, time: 2000},function(){layer.closeAll();});
            return;
        }

    }
    $(".selecttime button").on("click",function(){
        var val=$(this).index()+1;
        $("input[name='dateselect']").val(val);
        $(this).removeClass("layui-btn-primary").siblings().addClass("layui-btn-primary");
        if($(this).text()=="其他"){
            $("#top_time").show();
        }else{
            $("#top_time").hide();
            getpage(1);
        }
    });
    function output(obj){
        var keyword=$("#txtTopupSeachStr").val().length>0 ? $("#txtTopupSeachStr").val() : '';
        if(obj==0){
            window.open("{php echo $this->createWebUrl('sale',array('op'=>'outputs'))}&keyword="+keyword+"&datetype="+$("input[name='dateselect']").val()+"&statrtime="+$("input[name='gametime[start]']").val()+"&endtime="+$("input[name='gametime[end]']").val());
        }else{
            window.open("{php echo $this->createWebUrl('sale',array('op'=>'outputs_detail'))}&keyword="+keyword+"&datetype="+$("input[name='dateselect']").val()+"&statrtime="+$("input[name='gametime[start]']").val()+"&endtime="+$("input[name='gametime[end]']").val());
        }
    }
    function getpage(pageindex){
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        var keyword='';
        var searchdata={
            "keyword":keyword,
            "datetype":$("input[name='dateselect']").val(),
            "statrtime":$("input[name='gametime[start]']").val(),
            "endtime":$("input[name='gametime[end]']").val(),
            "page":pageindex,
        }
        $.post("{php echo $this->createWebUrl('elem',array('op'=>'salepage'))}",searchdata,function(returnstr){
            layer.close(index2);
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(!result.success){
                layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
                return;
            }
            if(pageindex==1){
                layui.use(['laypage'], function(){
                    var laypage = layui.laypage;
                    laypage.render({
                        elem: 'pagecount'
                        ,count: result.total
                        ,limit: 10
                        ,curr: 1
                        ,jump: function(obj, first){
                            var curr = obj.curr;
                            if(first==true)return;
                            getpage(curr);
                            return;
                        }
                    });
                });
                var item1=result.item1;
                var item2=result.item2;

                $("#box1_1").text(item1.num1);
                $("#box1_2").text(fee2num(item1.fee1));
                $("#box1_3").text(fee2num(item1.fee2));
                $("#box1_4").text(fee2num(item1.fee3));
                $("#box1_5").text(fee2num(item1.fee4));
                $("#box1_6").text(fee2num(parseInt(item1.fee1)/parseInt(item1.num1)*0.01));
                $("#box1_7").text(item2.num1);
            }
            $("#salelist").empty();
            if(result.success){
                var list=result.list;
                for(var i in list){
                    //单号，原价，实付，配送费，收入，时间，姓名，地址，状态，备注
                    var temp='<tr oid="'+list[i].orderid+'"><td style="font-size:70%">'+list[i].orderid+'</td>';
                    temp+='<td>'+fee2num(list[i].originalPrice)+'</td>';
                    temp+='<td>'+fee2num(list[i].totalPrice)+'</td>';
                    temp+='<td>'+fee2num(list[i].deliverFee)+'</td>';
                    temp+='<td>'+fee2num(list[i].income)+'</td>';
                    temp+='<td>'+list[i].realname+'<p>'+list[i].mobile+'</p></td>';
                    temp+='<td>'+fromatTime(list[i].createtime)+'</td>';
                    temp+='<td>'+list[i].deliveryPoiAddress+'</td>';
                    temp+='<td>'+(list[i].status=='settled' ? '<span class="layui-badge layui-bg-green">完成</span>': '<span class="layui-badge layui-bg-gray">无效</span>')+' '+(list[i].refundStatus=='noRefund' ? "" : '<span class="layui-badge">已退款</span>')+'</td>';

                    temp+='<td>'+list[i].description+'</td><td style="text-align:right"><a href="javascript:getOrderdetail(\''+list[i].orderid+'\')" class="layui-btn layui-btn-small">明细</a></td></tr>';
                    $("#salelist").append(temp);
                }
            }
        });
    }
    function getOrderdetail(id){
        if($("#salelist .detailbox").size()>0)$("#salelist .detailbox").remove();
        if($("#salelist tr[oid='"+id+"']").hasClass("isselect"))return;
        $("#salelist tr[oid='"+id+"']").addClass("isselect").siblings().removeClass("isselect");
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        $.post("{php echo $this->createWebUrl('elem',array('op'=>'getorderdetail'))}",{orderid:id},function(returnstr){
            layer.close(index2);
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(!result.success){
                layer.msg(result.msg ? result.msg : returnstr,{icon:2,time:2000});
                return;
            }
            if(result.success){
                var list=result.list;
                var temp="";
                for(var i in list){
                    //单号，原价，实付，配送费，收入，时间，姓名，地址，状态，备注
                    temp+='<tr class="detailbox"><td><i class="orange fa fa-menu-right"></i> '+list[i].title+'</td>';
                    temp+='<td>'+fee2num(list[i].price)+'</td><td>'+list[i].quantity+'</td><td><b>'+fee2num(list[i].total)+'</b></td>';
                    temp+='</tr>';
                }
                $(temp).insertAfter($("#salelist tr[oid='"+id+"']"));

            }
        });

    }
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var myDate = new Date();
        var datenow=myDate.toLocaleDateString();
        laydate.render({
            elem: '#LAY_demorange_s'
            ,min: '2016-01-01'
            ,max: '{php echo date("Y-m-d")}'
        });
        laydate.render({
            elem: '#LAY_demorange_e'
            ,min: '2016-01-01'
            ,max: '{php echo date("Y-m-d")}'
        });
    });
</script>

{/if}
